

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.dataset.RadsDsets &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.dataset.RadsDsets</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.dataset.RadsDsets</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with Frommle; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018</span>

<span class="kn">from</span> <span class="nn">geoslurp.dataset</span> <span class="k">import</span> <span class="n">DataSet</span>
<span class="kn">from</span> <span class="nn">geoalchemy2.elements</span> <span class="k">import</span> <span class="n">WKBElement</span>
<span class="kn">from</span> <span class="nn">geoalchemy2.types</span> <span class="k">import</span> <span class="n">Geography</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">Column</span><span class="p">,</span><span class="n">Integer</span><span class="p">,</span><span class="n">String</span><span class="p">,</span> <span class="n">Boolean</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.dialects.postgresql</span> <span class="k">import</span> <span class="n">TIMESTAMP</span><span class="p">,</span> <span class="n">JSONB</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">MetaData</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull</span> <span class="k">import</span> <span class="n">UriFile</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.rsync</span> <span class="k">import</span> <span class="n">Crawler</span> <span class="k">as</span> <span class="n">rsync</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.uri</span> <span class="k">import</span> <span class="n">findFiles</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="k">import</span> <span class="n">declared_attr</span><span class="p">,</span> <span class="n">as_declarative</span>
<span class="kn">from</span> <span class="nn">netCDF4</span> <span class="k">import</span> <span class="n">Dataset</span> <span class="k">as</span> <span class="n">ncDset</span>
<span class="kn">from</span> <span class="nn">osgeo</span> <span class="k">import</span> <span class="n">ogr</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span><span class="p">,</span><span class="n">timedelta</span>
<span class="kn">from</span> <span class="nn">glob</span> <span class="k">import</span> <span class="n">glob</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.catalogue</span> <span class="k">import</span> <span class="n">geoslurpCatalogue</span>
<span class="kn">from</span> <span class="nn">geoslurp.db.settings</span> <span class="k">import</span> <span class="n">getCreateDir</span>
<span class="n">geotracktype</span> <span class="o">=</span> <span class="n">Geography</span><span class="p">(</span><span class="n">geometry_type</span><span class="o">=</span><span class="s2">&quot;MULTILINESTRINGZ&quot;</span><span class="p">,</span> <span class="n">srid</span><span class="o">=</span><span class="s1">&#39;4326&#39;</span><span class="p">,</span> <span class="n">spatial_index</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">dimension</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>

<span class="nd">@as_declarative</span><span class="p">(</span><span class="n">metadata</span><span class="o">=</span><span class="n">MetaData</span><span class="p">(</span><span class="n">schema</span><span class="o">=</span><span class="s1">&#39;altim&#39;</span><span class="p">))</span>
<span class="k">class</span> <span class="nc">RadsTBase</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="c1">#strip of the &#39;Table&#39; from the class name</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">[:</span><span class="o">-</span><span class="mi">5</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">lastupdate</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">TIMESTAMP</span><span class="p">)</span>
    <span class="n">tstart</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">TIMESTAMP</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">tend</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">TIMESTAMP</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">cycle</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>
    <span class="n">apass</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>
    <span class="n">uri</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span><span class="n">index</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">data</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">JSONB</span><span class="p">)</span>
    <span class="n">geom</span><span class="o">=</span><span class="n">Column</span><span class="p">(</span><span class="n">geotracktype</span><span class="p">)</span>

<div class="viewcode-block" id="is_set"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.is_set">[docs]</a><span class="k">def</span> <span class="nf">is_set</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Check if the nth bit of x is set to True&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">x</span> <span class="o">&amp;</span> <span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="n">n</span> <span class="o">!=</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="flag4_isonLand"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.flag4_isonLand">[docs]</a><span class="k">def</span> <span class="nf">flag4_isonLand</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">is_set</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span></div>

<div class="viewcode-block" id="radsMetaDataExtractor"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.radsMetaDataExtractor">[docs]</a><span class="k">def</span> <span class="nf">radsMetaDataExtractor</span><span class="p">(</span><span class="n">uri</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Extract a dictionary with rads entries for the database&quot;&quot;&quot;</span>
    <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;extracting data from </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">))</span>
    <span class="n">ncrads</span><span class="o">=</span><span class="n">ncDset</span><span class="p">(</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
    <span class="n">track</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbMultiLineString</span><span class="p">)</span>
    <span class="n">data</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;segments&quot;</span><span class="p">:[]}</span>
   

    <span class="k">if</span> <span class="n">ncrads</span><span class="o">.</span><span class="n">dimensions</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">size</span> <span class="o">&lt;</span><span class="mi">3</span><span class="p">:</span>
       <span class="c1">#no point trying to index empty files</span>
       <span class="k">return</span> <span class="p">{}</span>
    <span class="c1">#reference time </span>
    <span class="n">t0</span><span class="o">=</span><span class="n">datetime</span><span class="p">(</span><span class="mi">1985</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
    
    <span class="c1"># We need to compare some values fromt he previous loop which we store in the follwoing variables </span>
    <span class="n">lonprev</span><span class="o">=</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;lon&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">lonprev</span> <span class="o">&gt;</span> <span class="mi">180</span><span class="p">:</span>
        <span class="n">lonprev</span><span class="o">-=</span><span class="mi">360</span>
    
    <span class="n">tprev</span><span class="o">=</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;time&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">onlandprev</span><span class="o">=</span><span class="n">flag4_isonLand</span><span class="p">(</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;flags&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
   
   <span class="c1">#initiate first linestring segment </span>
    <span class="n">trackseg</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
    <span class="c1">#we also store some bookkeeping information on each track segment</span>
    <span class="n">segment</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;tstart&quot;</span><span class="p">:(</span><span class="n">t0</span><span class="o">+</span><span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">tprev</span><span class="p">)))</span><span class="o">.</span><span class="n">isoformat</span><span class="p">(),</span><span class="s2">&quot;tend&quot;</span><span class="p">:</span><span class="kc">None</span><span class="p">,</span><span class="s2">&quot;istart&quot;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="s2">&quot;iend&quot;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="s2">&quot;land&quot;</span><span class="p">:</span><span class="nb">int</span><span class="p">(</span><span class="n">flag4_isonLand</span><span class="p">(</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;flags&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">]))}</span>
   
    
    <span class="k">for</span> <span class="n">i</span><span class="p">,(</span><span class="n">t</span><span class="p">,</span><span class="n">lon</span><span class="p">,</span><span class="n">lat</span><span class="p">,</span><span class="n">flag</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;time&quot;</span><span class="p">][:],</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;lon&quot;</span><span class="p">][:],</span><span class="n">ncrads</span><span class="p">[</span><span class="s2">&quot;lat&quot;</span><span class="p">][:],</span><span class="n">ncrads</span><span class="p">[</span><span class="s1">&#39;flags&#39;</span><span class="p">][:])):</span>
        <span class="n">dt</span><span class="o">=</span><span class="n">t0</span><span class="o">+</span><span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
        <span class="n">onland</span><span class="o">=</span><span class="n">flag4_isonLand</span><span class="p">(</span><span class="n">flag</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">lon</span> <span class="o">&gt;</span> <span class="mi">180</span><span class="p">:</span>
            <span class="c1">#make sure longitude goes from -180 to 180</span>
            <span class="n">lon</span><span class="o">-=</span><span class="mi">360</span>
            <span class="c1">#create a new segment when: (a) crossing the 180 d line, (b) a gap larger than 100 seconds is occurred, (c) or when ocean/land flag changes</span>
        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">lonprev</span><span class="o">-</span><span class="n">lon</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">180</span> <span class="ow">or</span> <span class="n">t</span><span class="o">-</span><span class="n">tprev</span> <span class="o">&gt;</span> <span class="mi">100</span> <span class="ow">or</span> <span class="p">(</span><span class="n">onlandprev</span> <span class="o">!=</span> <span class="n">onland</span><span class="p">):</span>
            <span class="c1">#start a new segment upon crossing the 180 line or when a time gap occurredi, or when crossing from land to ocean or lake</span>
            <span class="c1">#Segments which have more than a single point will be added:</span>
            <span class="k">if</span> <span class="n">trackseg</span><span class="o">.</span><span class="n">GetPointCount</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="c1">#gather some end bookkeeping on the previous segment</span>
                <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;tend&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
                <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;iend&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">i</span>
   
                <span class="c1">#append segment and bookkeeping data</span>
                <span class="n">data</span><span class="p">[</span><span class="s2">&quot;segments&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">segment</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span>
                <span class="c1"># import pdb;pdb.set_trace()</span>
                <span class="n">track</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">trackseg</span><span class="p">)</span>
            <span class="c1">#initialize new segment</span>
            <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;tstart&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
            <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;istart&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">i</span>
            <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;land&quot;</span><span class="p">]</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">onland</span><span class="p">)</span>
            <span class="n">trackseg</span><span class="o">=</span><span class="n">ogr</span><span class="o">.</span><span class="n">Geometry</span><span class="p">(</span><span class="n">ogr</span><span class="o">.</span><span class="n">wkbLineString</span><span class="p">)</span>
        
        <span class="n">trackseg</span><span class="o">.</span><span class="n">AddPoint</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">lon</span><span class="p">),</span><span class="nb">float</span><span class="p">(</span><span class="n">lat</span><span class="p">),</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">lonprev</span><span class="o">=</span><span class="n">lon</span>
        <span class="n">tprev</span><span class="o">=</span><span class="n">t</span>
        <span class="n">onlandprev</span><span class="o">=</span><span class="n">onland</span>
    
    <span class="c1">#also add the last segment</span>
    <span class="k">if</span> <span class="n">trackseg</span><span class="o">.</span><span class="n">GetPointCount</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="c1">#gather some end bookkeeping on the previous segment</span>
        <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;tend&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">dt</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
        <span class="n">segment</span><span class="p">[</span><span class="s2">&quot;iend&quot;</span><span class="p">]</span><span class="o">=</span><span class="n">i</span>

        <span class="c1">#append segment and bookkeeping data</span>
        <span class="n">data</span><span class="p">[</span><span class="s2">&quot;segments&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">segment</span><span class="p">)</span>
        <span class="n">track</span><span class="o">.</span><span class="n">AddGeometry</span><span class="p">(</span><span class="n">trackseg</span><span class="p">)</span>
   
    <span class="k">if</span> <span class="ow">not</span> <span class="n">data</span><span class="p">[</span><span class="s2">&quot;segments&quot;</span><span class="p">]:</span>
       <span class="c1">#return an empty dict when no segments are found</span>
       <span class="k">return</span> <span class="p">{}</span>

    <span class="c1">#reference time for rads</span>
    <span class="n">mtch</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;p([0-9]+)c([0-9]+).nc&quot;</span><span class="p">,</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
    <span class="n">meta</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;lastupdate&quot;</span><span class="p">:</span><span class="n">uri</span><span class="o">.</span><span class="n">lastmod</span><span class="p">,</span>
          <span class="s2">&quot;tstart&quot;</span><span class="p">:</span><span class="n">t0</span><span class="o">+</span><span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">ncrads</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">])),</span>
          <span class="s2">&quot;tend&quot;</span><span class="p">:</span><span class="n">t0</span><span class="o">+</span><span class="n">timedelta</span><span class="p">(</span><span class="n">seconds</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">ncrads</span><span class="p">[</span><span class="s1">&#39;time&#39;</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">])),</span>
          <span class="s2">&quot;cycle&quot;</span><span class="p">:</span><span class="nb">int</span><span class="p">(</span><span class="n">mtch</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)),</span>
          <span class="s2">&quot;apass&quot;</span><span class="p">:</span><span class="nb">int</span><span class="p">(</span><span class="n">mtch</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)),</span>
          <span class="s2">&quot;uri&quot;</span><span class="p">:</span><span class="n">uri</span><span class="o">.</span><span class="n">url</span><span class="p">,</span>
          <span class="s2">&quot;data&quot;</span><span class="p">:</span><span class="n">data</span><span class="p">,</span>
          <span class="s2">&quot;geom&quot;</span><span class="p">:</span><span class="n">WKBElement</span><span class="p">(</span><span class="n">track</span><span class="o">.</span><span class="n">ExportToIsoWkb</span><span class="p">(),</span><span class="n">srid</span><span class="o">=</span><span class="mi">4326</span><span class="p">,</span><span class="n">extended</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
          <span class="p">}</span>

    <span class="k">return</span> <span class="n">meta</span></div>


<div class="viewcode-block" id="RadsBase"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.RadsBase">[docs]</a><span class="k">class</span> <span class="nc">RadsBase</span><span class="p">(</span><span class="n">DataSet</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for a satellite + phase in the rads database</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">table</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">sat</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">phase</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">scheme</span><span class="o">=</span><span class="s1">&#39;altim&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dbconn</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">dbconn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updated</span><span class="o">=</span><span class="kc">None</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="p">:</span>
            <span class="k">if</span> <span class="s1">&#39;RADSDATAROOT&#39;</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="o">=</span><span class="n">getCreateDir</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;RADSDATAROOT&#39;</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">conf</span><span class="o">.</span><span class="n">getDir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span><span class="s2">&quot;DataDir&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updateInvent</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
        <span class="c1">#initialize postgreslq table</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">dbeng</span><span class="p">,</span><span class="n">checkfirst</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="c1"># RadsTBase.metadata.tables[&quot;.&quot;.join([self.scheme.lower(),self.name])].create(self.db.dbeng,checkfirst=True)</span>
        <span class="c1"># RadsTBase.metadata.create_all(self.db.dbeng, checkfirst=True)</span>

<div class="viewcode-block" id="RadsBase.pull"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.RadsBase.pull">[docs]</a>    <span class="k">def</span> <span class="nf">pull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cycle</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Pulls the data from the rads server</span>
<span class="sd">        :param cycle: only pulls data from a specific cycle</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cred</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">conf</span><span class="o">.</span><span class="n">authCred</span><span class="p">(</span><span class="s2">&quot;rads&quot;</span><span class="p">)</span>

        <span class="n">url</span><span class="o">=</span><span class="s2">&quot;rads.tudelft.nl::rads/data&quot;</span>

        <span class="c1">#pull configuration data (xml files)</span>
        <span class="n">rsync</span><span class="p">(</span><span class="n">url</span><span class="o">+</span><span class="s2">&quot;/conf&quot;</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="n">cred</span><span class="p">)</span><span class="o">.</span><span class="n">parallelDownload</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="p">,</span><span class="kc">True</span><span class="p">)</span>

        <span class="n">srcurl</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">sat</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">phase</span><span class="p">)</span>
        <span class="n">desturl</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">sat</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cycle</span><span class="p">:</span>
            <span class="n">srcurl</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">srcurl</span><span class="p">,</span><span class="s2">&quot;c</span><span class="si">%03d</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">cycle</span><span class="p">))</span>
            <span class="n">desturl</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">desturl</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">phase</span><span class="p">)</span>
        <span class="n">getCreateDir</span><span class="p">(</span><span class="n">desturl</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updated</span><span class="o">=</span><span class="n">rsync</span><span class="p">(</span><span class="n">srcurl</span><span class="p">,</span><span class="n">auth</span><span class="o">=</span><span class="n">cred</span><span class="p">)</span><span class="o">.</span><span class="n">parallelDownload</span><span class="p">(</span><span class="n">desturl</span><span class="p">,</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="RadsBase.register"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.RadsBase.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">cycle</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">since</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">since</span><span class="p">:</span>
           <span class="n">since</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">since</span><span class="p">,</span><span class="s2">&quot;%Y-%m-</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
           <span class="nb">print</span><span class="p">(</span><span class="n">since</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
           <span class="n">since</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">lastupdate</span>
        <span class="c1">#create a list of files which need to be (re)registered</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">updated</span><span class="p">:</span>
            <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">updated</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Listing files to process (this can take a while)...&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">cycle</span><span class="p">:</span>
                <span class="n">files</span><span class="o">=</span><span class="p">[</span><span class="n">UriFile</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">findFiles</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">sat</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">phase</span><span class="p">,</span><span class="s2">&quot;c</span><span class="si">%03d</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">cycle</span><span class="p">)),</span><span class="s1">&#39;.*\.nc$&#39;</span><span class="p">,</span><span class="n">since</span><span class="o">=</span><span class="n">since</span><span class="p">)]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">files</span><span class="o">=</span><span class="p">[</span><span class="n">UriFile</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">findFiles</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dbinvent</span><span class="o">.</span><span class="n">datadir</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">sat</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">phase</span><span class="p">),</span><span class="s1">&#39;.*\.nc$&#39;</span><span class="p">,</span><span class="n">since</span><span class="o">=</span><span class="n">since</span><span class="p">)]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">files</span><span class="p">:</span>
           <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;No updated files found&quot;</span><span class="p">)</span>
           <span class="k">return</span>

        <span class="n">newfiles</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">retainnewUris</span><span class="p">(</span><span class="n">files</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">newfiles</span><span class="p">:</span>
            <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Nothing to update&quot;</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="k">for</span> <span class="n">uri</span> <span class="ow">in</span> <span class="n">newfiles</span><span class="p">:</span>
            <span class="n">meta</span><span class="o">=</span><span class="n">radsMetaDataExtractor</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">meta</span><span class="p">:</span>
               <span class="c1">#don&#39;t register empty entries</span>
               <span class="k">continue</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">addEntry</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">updateInvent</span><span class="p">()</span></div></div>






<span class="c1"># Factory method to dynamically create classes</span>
<div class="viewcode-block" id="radsclassFactory"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.radsclassFactory">[docs]</a><span class="k">def</span> <span class="nf">radsclassFactory</span><span class="p">(</span><span class="n">clnm</span><span class="p">):</span>
    <span class="n">dum</span><span class="p">,</span><span class="n">sat</span><span class="p">,</span><span class="n">phase</span><span class="o">=</span><span class="n">clnm</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;_&quot;</span><span class="p">)</span>
    <span class="n">table</span><span class="o">=</span><span class="nb">type</span><span class="p">(</span><span class="n">clnm</span><span class="o">+</span><span class="s2">&quot;Table&quot;</span><span class="p">,(</span><span class="n">RadsTBase</span><span class="p">,),{})</span>
    <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">clnm</span><span class="p">,</span> <span class="p">(</span><span class="n">RadsBase</span><span class="p">,),</span> <span class="p">{</span><span class="s2">&quot;sat&quot;</span><span class="p">:</span><span class="n">sat</span><span class="p">,</span><span class="s2">&quot;phase&quot;</span><span class="p">:</span><span class="n">phase</span><span class="p">,</span><span class="s2">&quot;table&quot;</span><span class="p">:</span><span class="n">table</span><span class="p">})</span></div>

<div class="viewcode-block" id="getRADSDsets"><a class="viewcode-back" href="../../../reference/geoslurp.dataset.html#geoslurp.dataset.RadsDsets.getRADSDsets">[docs]</a><span class="k">def</span> <span class="nf">getRADSDsets</span><span class="p">(</span><span class="n">conf</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Create all tables for all satellite missions and phases&quot;&quot;&quot;</span>
    <span class="n">satphases</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;j1&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span><span class="s2">&quot;b&quot;</span><span class="p">,</span><span class="s2">&quot;c&quot;</span><span class="p">],</span><span class="s2">&quot;j2&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span><span class="s2">&quot;b&quot;</span><span class="p">,</span><span class="s2">&quot;c&quot;</span><span class="p">],</span><span class="s2">&quot;j3&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">],</span><span class="s2">&quot;3a&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">],</span><span class="s2">&quot;c2&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">],</span><span class="s2">&quot;n1&quot;</span><span class="p">:[</span><span class="s2">&quot;b&quot;</span><span class="p">,</span><span class="s2">&quot;c&quot;</span><span class="p">],</span><span class="s2">&quot;sa&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span><span class="s2">&quot;b&quot;</span><span class="p">],</span><span class="s2">&quot;tx&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span><span class="s2">&quot;b&quot;</span><span class="p">,</span><span class="s2">&quot;n&quot;</span><span class="p">],</span><span class="s2">&quot;3a&quot;</span><span class="p">:[</span><span class="s2">&quot;a&quot;</span><span class="p">]}</span>
    <span class="n">out</span><span class="o">=</span><span class="p">[]</span>
    <span class="k">for</span> <span class="n">sat</span><span class="p">,</span><span class="n">phases</span> <span class="ow">in</span> <span class="n">satphases</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">for</span>  <span class="n">phase</span> <span class="ow">in</span> <span class="n">phases</span><span class="p">:</span>
            <span class="n">clname</span><span class="o">=</span><span class="s2">&quot;rads_&quot;</span><span class="o">+</span><span class="n">sat</span><span class="o">+</span><span class="s2">&quot;_&quot;</span><span class="o">+</span><span class="n">phase</span>
            <span class="n">out</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">radsclassFactory</span><span class="p">(</span><span class="n">clname</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">out</span></div>

<span class="n">geoslurpCatalogue</span><span class="o">.</span><span class="n">addDatasetFactory</span><span class="p">(</span><span class="n">getRADSDsets</span><span class="p">)</span>
</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>